﻿using System;
using System.Configuration;
using System.Text;
using HelpdeskServices.CrmWebReference;
using HelpdeskServices.Properties;

public class Email
{
    private Guid _emailId;
    private activityparty _from;
    private string _subject;
    private email localEmail;
    private int _orderNumber;
    private CrmDateTime _receivedOn;

    public CrmDateTime ReceivedOn
    {
        get
        {
            if (_receivedOn == null)
            {
                _receivedOn = localEmail.createdon;
            }
            return _receivedOn;
        }
    }
    public Guid EmailId
    {
        get { return _emailId; }
        set { _emailId = value; }
    }

    public string Subject
    {
        get
        {
            if (_subject == null)
            {
                _subject = localEmail.subject;
            }
            return _subject;
        }
        set { _subject = value; }
    }

    public string FromEmailAddress
    {
        get
        {
            if (_from == null)
            {
                _from = localEmail.from[0];
            }
            return _from.addressused;
        }
    }
    public int OrderNumber
    {
        get
        {
         
            _orderNumber = Common.findOrderNumberInEmailBody(localEmail.description);
            return _orderNumber;
        }
        set { _orderNumber = value; }
    }
    private int parseEmailForOrderNumber()
    {
        return Common.findOrderNumberInEmailBody(localEmail.description);
    }
    public void Load()
    {
        TargetRetrieveEmail target = new TargetRetrieveEmail();
        target.EntityId = _emailId;

        RetrieveRequest retrieve = new RetrieveRequest();
        retrieve.Target = target;
        retrieve.ColumnSet = new AllColumns();
        CrmService crmServiceProxy = Common.CrmWebServiceProxy();
        RetrieveResponse response = (RetrieveResponse) crmServiceProxy.Execute(retrieve);
        localEmail = (email) response.BusinessEntity;
    }
    public override string ToString()
    {
        StringBuilder emailInformation = new StringBuilder();

        foreach (activityparty person in localEmail.cc)
        {
            emailInformation.Append(person.addressused);
        }
        foreach (activityparty person in localEmail.bcc)
        {
            emailInformation.Append(person.addressused);
        }
        emailInformation.AppendLine();
       emailInformation.AppendFormat("{0} {1}", Resources.EmailDescriptionPrefix,
                                      Common.removeHTMLTags(localEmail.description));
        emailInformation.AppendLine();
        emailInformation.AppendFormat("{0} {1}", Resources.EmailSubjectPrefix, Common.removeHTMLTags(localEmail.subject));
        emailInformation.AppendLine();
        emailInformation.AppendFormat("{0} {1}", Resources.EmailFromPrefix, localEmail.sender);

        return emailInformation.ToString();
    }
    public void Route()
    {
        TargetQueuedEmail target = new TargetQueuedEmail();
        target.EntityId = _emailId;
        RouteRequest request = new RouteRequest();
        request.Target = target;
        request.RouteType = RouteType.Auto;
        request.SourceQueueId =
            new Guid(ConfigurationManager.AppSettings[Resources.ConfigurationKeyForSupportQueueId].ToString());

        CrmService microsoftCrmWebService = Common.CrmWebServiceProxy();

        microsoftCrmWebService.Execute(request);
    }
}